package com.kevin.mapping.script.node.logical;

import com.kevin.mapping.script.engine.JSONEnv;
import com.kevin.mapping.script.exception.ExecuteException;
import com.kevin.mapping.script.lexer.BoolValue;
import com.kevin.mapping.script.lexer.Token;
import com.kevin.mapping.script.lexer.Type;
import com.kevin.mapping.script.node.stmt.Stmt;

public class Ne extends Logical {


    public Ne(Token token, Stmt left, Stmt right) {
        super(token, left, right);
    }

    @Override
    public Token execute(JSONEnv env) {
        Token leftRet = this.getLeft().execute(env);
        Token rightRet = this.getRight().execute(env);
        int type = Math.max(leftRet.type, rightRet.type);
        switch (type) {
            case Type.INTEGER:
                return new BoolValue(Type.INTEGER, !leftRet.getIntValue(env).equals(rightRet.getIntValue(env)));
            case Type.LONG:
                return new BoolValue(Type.LONG, !leftRet.getLongValue(env).equals(rightRet.getLongValue(env)));
            case Type.DOUBLE:
                return new BoolValue(Type.DOUBLE, !leftRet.getDoubleValue(env).equals(rightRet.getDoubleValue(env)));
            case Type.BIG_DECIMAL:
                return new BoolValue(Type.STR_VALUE, !leftRet.getBigDecimalValue(env).equals(rightRet.getBigDecimalValue(env)));
            default:
                throw new ExecuteException("unsuported type" + type);
        }
    }
}
